package com.ubis.ibms.plugin.composites.company;

import java.util.List;

import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.viewers.ArrayContentProvider;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Text;
import org.eclipse.wb.swt.SWTResourceManager;

import com.ubis.ibms.plugin.common.constants.Constants;
import com.ubis.ibms.plugin.common.viewer.ComboLabelProvider;
import com.ubis.ibms.plugin.common.viewer.TableContentProvider;
import com.ubis.ibms.plugin.common.viewer.TableLabelProvider;
import com.ubis.ibms.plugin.dao.CompanyManager;
import com.ubis.ibms.plugin.dao.EstateManager;
import com.ubis.ibms.plugin.dialogs.CompanyAddDialog;
import com.ubis.ibms.plugin.model.Building;
import com.ubis.ibms.plugin.model.Company;
import com.ubis.ibms.plugin.model.Floor;
import com.ubis.ibms.plugin.model.Room;

/**
 * 业主查询
 * @author diamond-wang
 *
 */
public class CompanyQueryComposite extends Composite {
	
	//公司名称
	private Text nameText;
	//公司编号
	private Text idText;
	//楼栋
	private ComboViewer buildingComboViewer;
	private Combo buildingCombo;
	//楼层
	private ComboViewer floorComboViewer;
	private Combo floorCombo;
	//房间
	private ComboViewer roomComboViewer;
	private Combo roomCombo;
	//联系人
	private Text perText;
	//查询
	private Button queryButton;
	//清空
	private Button clearButton;
	//登记
	private Button addButton;
	
	private Table table;
	private TableViewer tableViewer;

	/**
	 * Create the composite.
	 * @param parent
	 * @param style
	 */
	public CompanyQueryComposite(Composite parent, int style) {
		super(parent, style);
		
		GridLayout layout = new GridLayout(1, false);
		layout.marginWidth = 10;
		layout.marginHeight = 10;
		setLayout(layout);
		
		Composite titleComposite = new Composite(this, SWT.NONE);
		titleComposite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false, 1, 1));
		titleComposite.setLayout(new GridLayout(2, false));
		
		Label title = new Label(titleComposite, SWT.NONE);
		title.setFont(SWTResourceManager.getFont("微软雅黑", 9, SWT.BOLD));
		title.setText("业主查询");
		
		addButton = new Button(titleComposite, SWT.NONE);
		addButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				// 点击添加时
				CompanyAddDialog addDialog = new CompanyAddDialog(CompanyQueryComposite.this.getShell());
				addDialog.open();
			}
		});
		addButton.setText("登记");
		
		Composite queryComposite = new Composite(this, SWT.BORDER);
		queryComposite.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		queryComposite.setLayout(new GridLayout(6, false));

		// 楼栋
		Label buildingLabel = new Label(queryComposite, SWT.RIGHT);
		buildingLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		buildingLabel.setText("楼栋：");
		buildingComboViewer = new ComboViewer(queryComposite, SWT.READ_ONLY);
		buildingCombo = buildingComboViewer.getCombo();
		buildingCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		buildingComboViewer.setContentProvider(ArrayContentProvider
				.getInstance());
		buildingComboViewer.setLabelProvider(new ComboLabelProvider());
		buildingComboViewer.setInput(Constants.BUILDINGS);

		buildingComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						System.out.println("buildingComboViewer selection changed.");
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
						if(selection.isEmpty())
							return;
						String buildingCode = ((Building) selection
								.getFirstElement()).getEms_id();
						// 获取楼层数据
						List<Floor> floors = EstateManager
								.getFloorsByBuilding(buildingCode);
						floorComboViewer.setInput(floors);
						roomComboViewer.setInput(null);
					}
				});
		
		//楼层
		Label floorLabel = new Label(queryComposite, SWT.RIGHT);
		floorLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		floorLabel.setText("楼层：");
		floorComboViewer = new ComboViewer(queryComposite, SWT.READ_ONLY);
		floorCombo = floorComboViewer.getCombo();
		floorCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		floorComboViewer.setContentProvider(ArrayContentProvider.getInstance());
		floorComboViewer.setLabelProvider(new ComboLabelProvider());

		floorComboViewer
				.addSelectionChangedListener(new ISelectionChangedListener() {
					@Override
					public void selectionChanged(SelectionChangedEvent event) {
						IStructuredSelection selection = (IStructuredSelection) event
								.getSelection();
						if(selection.isEmpty())
							return;
						//获取楼编号
						IStructuredSelection buildingSelection = (IStructuredSelection) buildingComboViewer
								.getSelection();
						String buildingCode = ((Building) buildingSelection.getFirstElement())
								.getEms_id();
						//获取层编号
						String floorCode = ((Floor) selection.getFirstElement())
								.getEms_id();
						// 获取楼层数据
						List<Room> rooms = EstateManager.getRoomsByBuildingAndFloor(buildingCode, floorCode);
						roomComboViewer.setInput(rooms);
						
					}
				});
		
		//房间号
		Label roomLabel = new Label(queryComposite, SWT.RIGHT);
		roomLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false,
				false, 1, 1));
		roomLabel.setText("房间：");
		roomComboViewer = new ComboViewer(queryComposite, SWT.READ_ONLY);
		roomCombo = roomComboViewer.getCombo();
		roomCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		roomComboViewer.setContentProvider(ArrayContentProvider.getInstance());
		roomComboViewer.setLabelProvider(new ComboLabelProvider());
		// roomComboViewer
		// .addSelectionChangedListener(new ISelectionChangedListener() {
		// @Override
		// public void selectionChanged(SelectionChangedEvent event) {
		// IStructuredSelection selection = (IStructuredSelection) event
		// .getSelection();
		// System.out.println(((Room) selection
		// .getFirstElement()).getId());
		// }
		// });
		
		//公司名称
		Label nameLabel1 = new Label(queryComposite, SWT.RIGHT);
		nameLabel1.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		nameLabel1.setText("公司名称：");
		nameText = new Text(queryComposite, SWT.BORDER);
		nameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		//联系人
		Label perLabel = new Label(queryComposite, SWT.RIGHT);
		perLabel.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		perLabel.setText("联系人：");
		perText = new Text(queryComposite, SWT.BORDER);
		perText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));
		
		//点击查询按钮的动作
		queryButton = new Button(queryComposite, SWT.NONE);
		queryButton.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				System.out.println("queryButton mouse up.");
				//若有公司名，一切按照公司名查找。否则，若有联系人，按联系人查找
				//若没有公司名，也没有联系人，则按照楼层查找
				//楼，楼层，房间号必须全部填写才能查询
				if(nameText!=null && nameText.getText().length() >0){
					tableViewer.setInput(CompanyManager.findByName(nameText.getText()));
				}else if(perText!= null && perText.getText().length() >0){
					tableViewer.setInput(CompanyManager.findByPer(perText.getText()));
				}else if(roomCombo.getText()!=null && roomCombo.getText().length()>0){
					//System.out.println("index"+roomCombo.getSelectionIndex());
					IStructuredSelection selection = (IStructuredSelection)roomComboViewer.getSelection();
					Room room = (Room)selection.getFirstElement();
					Long room_moi_id = room.getId();
//					Long room_moi_id = rooms.get(roomCombo.getSelectionIndex()).getId();
					tableViewer.setInput(CompanyManager.findByRoom(room_moi_id));
				}else{
					MessageDialog.openWarning(CompanyQueryComposite.this.getShell(), "警告", "您输入的查询条件不合法！");
				}
			}
		});
		queryButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		queryButton.setText("查询");
		
		//点击清空按钮的动作
		clearButton = new Button(queryComposite, SWT.NONE);
		clearButton.addMouseListener(new MouseAdapter() {
			public void mouseUp(MouseEvent e) {
				clearInput();//清空输入
			}
		});
		clearButton.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1));
		clearButton.setText("清空");
		
//		tableViewerColumn = new TableViewerColumn(tableViewer_1, SWT.NONE);
//		tblclmnNewColumn = tableViewerColumn.getColumn();
//		tblclmnNewColumn.setWidth(100);
//		tblclmnNewColumn.setText("New Column");
//		new Label(queryComposite, SWT.NONE);
//		new Label(queryComposite, SWT.NONE);
//		new Label(queryComposite, SWT.NONE);
//		new Label(queryComposite, SWT.NONE);
//		new Label(queryComposite, SWT.NONE);

		tableViewer = new TableViewer(this, SWT.BORDER | SWT.FULL_SELECTION);
		table = tableViewer.getTable();
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		table.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
		
		TableViewerColumn VcompanyIdColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn companyIdColumn = VcompanyIdColumn.getColumn();
		companyIdColumn.setResizable(false);
		companyIdColumn.setAlignment(SWT.CENTER);
		companyIdColumn.setWidth(150);
		companyIdColumn.setText("公司编号");
		
		TableViewerColumn VcompanyNameColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn companyNameColumn = VcompanyNameColumn.getColumn();
		companyNameColumn.setResizable(false);
		companyNameColumn.setAlignment(SWT.CENTER);
		companyNameColumn.setWidth(150);
		companyNameColumn.setText("公司名称");
		
		TableViewerColumn VcompanyPerColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn companyPerColumn = VcompanyPerColumn.getColumn();
		companyPerColumn.setResizable(false);
		companyPerColumn.setAlignment(SWT.CENTER);
		companyPerColumn.setWidth(150);
		companyPerColumn.setText("联系人");
		
		TableViewerColumn VcompanyTelColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn companyTelColumn = VcompanyTelColumn.getColumn();
		companyTelColumn.setResizable(false);
		companyTelColumn.setAlignment(SWT.CENTER);
		companyTelColumn.setWidth(150);
		companyTelColumn.setText("手机");
		
		TableViewerColumn VcompanyPhoneColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn companyPhoneColumn = VcompanyPhoneColumn.getColumn();
		companyPhoneColumn.setResizable(false);
		companyPhoneColumn.setAlignment(SWT.CENTER);
		companyPhoneColumn.setWidth(150);
		companyPhoneColumn.setText("座机");
		
		TableViewerColumn VcompanyBalanceColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn companyBalanceColumn = VcompanyBalanceColumn.getColumn();
		companyBalanceColumn.setResizable(false);
		companyBalanceColumn.setAlignment(SWT.CENTER);
		companyBalanceColumn.setWidth(150);
		companyBalanceColumn.setText("预存款");
		
		TableViewerColumn VcompanyRemindDayColumn = new TableViewerColumn(tableViewer, SWT.NONE);
		TableColumn companyRemindDayColumn = VcompanyRemindDayColumn.getColumn();
		companyRemindDayColumn.setResizable(false);
		companyRemindDayColumn.setAlignment(SWT.CENTER);
		companyRemindDayColumn.setWidth(150);
		companyRemindDayColumn.setText("提醒阈值天数");
        
        tableViewer.setContentProvider(new TableContentProvider());
		tableViewer.setLabelProvider(new TableLabelProvider());
		tableViewer.setInput(null);
		//tableViewer.setInput(arrayCompanies);
		//双击某条记录时，进入该条记录的信息视图
		tableViewer.addDoubleClickListener(new IDoubleClickListener(){
			public void doubleClick(DoubleClickEvent event) {
				//返回选中行的company
				IStructuredSelection selection = (IStructuredSelection)tableViewer.getSelection();
				Company company = (Company)selection.getFirstElement();
				//如果选中行不是空白行,将company参数传到context中，跳到company info的视图
				if(company!=null){
					Composite parent = CompanyQueryComposite.this.getParent();
					parent.getChildren()[1].dispose();
					Composite composite = new CompanyInfoComposite(parent, SWT.NONE, company);
					composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1));
					parent.layout();
//					application.getContext().set("company", company);//传递company参数
					//切换到company info perspective
//					PerspectiveUtils.switchToPerspective(application, modelService, partService, "ibms_rcp.perspective.company.info");
				}
				System.out.println("QueryPart tableviewer double click called.");
			}
			
		});
	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	/**
	 * 清空输入
	 */
	public void clearInput(){
		buildingComboViewer.setSelection(null);
		floorComboViewer.setSelection(null);
		roomComboViewer.setSelection(null);
		perText.setText("");
		nameText.setText("");
		tableViewer.setInput(null);
	}
}
